package livesun.justiceeternal.persenter;
import android.content.Context;
import java.util.concurrent.TimeUnit;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
import livesun.justiceeternal.adapter.ScoreBean;
import livesun.justiceeternal.base.Contract;
import livesun.justiceeternal.contract.IGetScoreContract;
import livesun.justiceeternal.retorfitP.Request;
import livesun.justiceeternal.retorfitP.RetrofitUtil;
/**
 * Created by 29028 on 2017/8/11.
 */
public class GetScorePersenter implements IGetScoreContract.Persenter {

    private IGetScoreContract.View mView;
    private int page;
    private CompositeDisposable mCompositeDisposable = new CompositeDisposable();
    public GetScorePersenter(IGetScoreContract.View view, Context context){
        this.mView = view;
        mView.setPersenter(this);
    }

    @Override
    public void start() {
        page = 1;
        String url= "search/issues?q="+mView.getKeyWords()+"+state:open+repo:zytx121/je/sort=created&order=desc&page="+page+"&per_page=15!";


        DisposableObserver<ScoreBean> observer = new DisposableObserver<ScoreBean>() {


            @Override
            public void onNext(ScoreBean scoreBean) {
                mView.getDatasSucess(scoreBean);
            }

            @Override
            public void onError(Throwable e) {
                mView.getDataError(e.getMessage());
            }

            @Override
            public void onComplete() {

            }
        };
        RetrofitUtil.getInstance().get(Request.class)
                .start(url)
                .timeout(15,TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);

        mCompositeDisposable.add(observer);

    }

    @Override
    public void loardMore() {
        int laodPage=++page;
        String url= "search/issues?q="+mView.getKeyWords()+"+state:open+repo:"+Contract.CONTEXT+"/sort=created&order=desc&page="+laodPage+"&per_page=15!";

        DisposableObserver<ScoreBean> observer = new DisposableObserver<ScoreBean>() {
            @Override
            public void onNext(ScoreBean scoreBean) {
                mView.loadMoreSucess(scoreBean);
            }

            @Override
            public void onError(Throwable e) {
                mView.loardMoreError(e.getMessage());
            }

            @Override
            public void onComplete() {

            }
        };
        RetrofitUtil.getInstance().get(Request.class)
                .loardMore(url)
                .timeout(15,TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);

        mCompositeDisposable.add(observer);
    }

    @Override
    public void cancel() {
        mCompositeDisposable.clear();
    }


}
